package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.FindOrderDetail;
import com.ramos.kuaixiao.entity.OrderDetail;
import com.ramos.kuaixiao.ui.UnSubmitOrderDetailActivity;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.util.List;

public class UnSubmitOrderAdapter extends BaseAdapter {

    private List<FindOrderDetail> tasklist;
    private Context context;
    private List<String> detaillist;
    private List<OrderDetail> orderllist;

    static class ViewHolder {
        TextView tvDanju;
        TextView tvKehu;
        TextView tvShijian;
        TextView tvMoney;
        TextView danhao;
        TextView num;
        RecyclerView image_ll;
        RelativeLayout image_rl;
    }

    public UnSubmitOrderAdapter(Context context, List<FindOrderDetail> hotcourse) {
        this.context = context;
        this.tasklist = hotcourse;
    }

    public void refresh(List<FindOrderDetail> data) {
        tasklist = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tasklist.size();
    }

    @Override
    public Object getItem(int position) {
        return tasklist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.listitem_unsubmit_order, null);
            holder = new ViewHolder();
            holder.tvDanju = (TextView) convertView.findViewById(R.id.danju);
            holder.tvKehu = (TextView) convertView.findViewById(R.id.kehu);
            holder.tvShijian = (TextView) convertView.findViewById(R.id.shijian);
            holder.tvMoney = (TextView) convertView.findViewById(R.id.state);
            holder.danhao = (TextView) convertView.findViewById(R.id.danhao);
            holder.num = (TextView) convertView.findViewById(R.id.num);
            holder.image_ll = (RecyclerView) convertView.findViewById(R.id.image_ll);
            holder.image_rl= (RelativeLayout) convertView.findViewById(R.id.image_rl);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDanju.setText("合计:￥" + App.df.format(Double.valueOf(tasklist.get(position).getOrderTotalPrice())));
        if (StringUtil.isEmpty(tasklist.get(position).getHouseName())) {
            holder.tvKehu.setText(tasklist.get(position).getCustmerName());
        } else {
            holder.tvKehu.setText(tasklist.get(position).getHouseName());
        }
        Drawable icon = context.getResources().getDrawable(R.drawable.arp);
        //setBounds(left,top,right,bottom)里的参数从左到右分别是
        //drawable的左边到textview左边缘+padding的距离，drawable的上边离textview上边缘+padding的距离
        //drawable的右边边离textview左边缘+padding的距离，drawable的下边离textview上边缘+padding的距离
        //所以right-left = drawable的宽，top - bottom = drawable的高
        icon.setBounds(20, 0, 70, 50);
        holder.tvKehu.setCompoundDrawables(icon, null, null, null);
        holder.tvShijian.setText("" + tasklist.get(position).getOrderDate());
        Log.d("SendOrderActivity", "Utils.getDate()：" + tasklist.get(position).getOrderDate());
        if (StringUtil.isEmpty(tasklist.get(position).getOrderDetailList())) {
            holder.num.setText("共0件商品");
        } else {
            orderllist = JSONArray.parseArray(tasklist.get(position).getOrderDetailList(), OrderDetail.class);
            if (orderllist == null || orderllist.size() == 0) {
                holder.num.setText("共0件商品");
            } else {
                holder.num.setText("共" + orderllist.size() + "件商品");
            }
        }
        detaillist = tasklist.get(position).getOrderImageList();
        if (holder.image_ll != null) {
            if (detaillist == null || detaillist.size() == 0) {
                holder.image_ll.setVisibility(View.GONE);
            } else {
                holder.image_ll.setVisibility(View.VISIBLE);
                showGrid(holder.image_ll, tasklist.get(position).getOrderImageList());
            }
        }
   /*     holder.image_ll.requestDisallowInterceptTouchEvent(false);
        holder.image_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("order", tasklist.get(position));
                context.startActivity(intent);
            }
        });*/
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UnSubmitOrderDetailActivity.class);
                intent.putExtra("order", tasklist.get(position));
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    private void showGrid(RecyclerView gridView, List<String> detaillist) {
        try {
            if (detaillist != null && detaillist.size() > 0) {
                LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(context);
                mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                gridView.setLayoutManager(mLinearLayoutManager);
                GridViewAdapter adapter = new GridViewAdapter(context,
                        detaillist);
                gridView.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * GirdView 数据适配器
     */
    public class GridViewAdapter extends RecyclerView.Adapter {
        Context context;
        List<String> list;

        public GridViewAdapter(Context _context, List<String> _list) {
            this.list = _list;
            this.context = _context;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.order_add_view_pic_item, null);
            Hodler vh = new Hodler(view);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(lp);
            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final Hodler viewHolder = (Hodler) holder;
            String path = list.get(position);
            if (viewHolder.ItemImage != null) {
                if (!StringUtil.isEmpty(path)) {
                    Utils.glideImage(path, viewHolder.ItemImage);
                } else {
                    viewHolder.ItemImage.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    class Hodler extends RecyclerView.ViewHolder {

        ImageView ItemImage;

        public Hodler(View convertView) {
            super(convertView);
            ItemImage = (ImageView) convertView.findViewById(R.id.ItemImage);
        }
    }
}